package com.io.io03.oj;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;

public class Main {
    private static Solution solution = new Solution();

    public static void main(String[] args) throws Exception {
        File inputFile = new File("D:\\课程\\OJ\\input.txt");
        File resultFile = new File("D:\\课程\\OJ\\output.txt");

        try (InputStream input = new FileInputStream(inputFile)) {
            try (InputStream result = new FileInputStream(resultFile)) {
                try (Scanner inputScanner = new Scanner(input, "UTF-8")) {
                    try (Scanner resultScanner = new Scanner(result, "UTF-8")) {
                        while (inputScanner.hasNextLine()) {
                            String line = inputScanner.nextLine();
                            String resultLine = resultScanner.nextLine();

                            test(line, resultLine);
                        }
                    }
                }
            }
        }
        System.out.println("测试用例全部通过");
    }

    private static void test(String inputLine, String resultLine) {
        // 从 inputLine 中构造链表（不断用尾插进行构建）
        ListNode head = new ListNode(-1);
        ListNode last = head;

        String[] array = inputLine.split(" ");  // 按空格分开
        for (String s : array) {
            int val = Integer.parseInt(s);  // 把 string 变成 int
            ListNode node = new ListNode(val);
            last.next = node;
            last = node;
        }

        try {
            ListNode reverseHead = solution.reverseList(head.next);
            // 对比 reverseHead 构造的链表和 resultLine 是否一致
            StringBuilder sb = new StringBuilder();
            for (ListNode cur = reverseHead; cur != null; cur = cur.next) {
                sb.append(cur.val).append(" ");
            }
            if (sb.length() > 0) {
                sb.delete(sb.length() - 1, sb.length());
            }
            String actualLine = sb.toString();

            if (!actualLine.equals(resultLine)) {
                System.out.println("输入: " + inputLine);
                System.out.println("期望输出: " + resultLine);
                System.out.println("实际输出: " + actualLine);
                System.exit(1);
            }
        } catch (Exception exc) {
            System.out.println("输入: " + inputLine);
            System.out.println("期望输出: " + resultLine);
            System.out.println("实际输出: ");
            throw new RuntimeException(exc);
        }
    }
}
